package com.spark.client.config;

import com.spark.client.SparkClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 星火客户端兼容性配置类
 * 支持 Spring Boot 2.x 和 3.x
 */
@Configuration
@EnableConfigurationProperties(SparkConfig.class)
@ConditionalOnClass(SparkClient.class)
public class SparkCompatibilityConfiguration {
    
    private static final Logger logger = LoggerFactory.getLogger(SparkCompatibilityConfiguration.class);

    /**
     * 创建 SparkClient Bean
     * 兼容 Spring Boot 2.x 和 3.x
     * @param config 配置对象
     * @return SparkClient
     */
    @Bean
    @ConditionalOnMissingBean
    public SparkClient sparkClient(SparkConfig config) {
        logger.info("初始化 SparkClient (兼容模式)，模型: {}, 基础URL: {}", config.getModel(), config.getBaseUrl());
        
        if (config.getApiKey() == null || config.getApiKey().trim().isEmpty()) {
            logger.warn("未配置 API Key，请在 application.yml 中设置 spark.api-key");
        }
        
        return new SparkClient(config);
    }
}
